package com.ximalaya.demo;

/**
 * Time: 2022/3/29 3:55 下午
 * Author: leixun
 * Email: leixun33@163.com
 * <p>
 * Description:
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * 通用适配器（适合一些常规的适配器）
 * @author Jenly
 *
 * @param <T>
 */
public abstract class HolderAdapter<T> extends AbstractAdapter<T>{

    public HolderAdapter(Context context, List<T> listData) {
        super(context, listData);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Object holder = null;
        if(convertView==null){
            convertView = buildConvertView(layoutInflater);
            holder = buildHolder(convertView);

            convertView.setTag(holder);
        }else{
            holder = convertView.getTag();
        }
        bindViewData(holder,listData.get(position),position);

        return convertView;
    }

    /**
     * 建立convertView
     * @param layoutInflater
     * @return
     */
    public abstract View buildConvertView(LayoutInflater layoutInflater);

    /**
     * 建立视图Holder
     * @param convertView
     * @return
     */
    public abstract Object buildHolder(View convertView);

    /**
     * 绑定数据
     * @param holder
     * @param t
     * @param position
     */
    public abstract void bindViewData(Object holder,T t,int position);


}